package com.sskj.b70.v.fragment;

import android.support.v4.app.Fragment;
import android.view.View;

import com.flyco.tablayout.SlidingTabLayout;
import com.sskj.b70.R;
import com.sskj.b70.adapter.MyFragmentPagerAdapter;
import com.sskj.b70.common.Constants;
import com.sskj.b70.common.rxbus2.RxBus;
import com.sskj.b70.common.rxbus2.Subscribe;
import com.sskj.b70.common.rxbus2.ThreadMode;
import com.sskj.b70.p.fragment.MarketFragmentPst;
import com.sskj.b70.v.activity.MainActivity;
import com.sskj.b70.v.base.BaseFragment;
import com.sskj.b70.v.widget.ViewPagerSlide;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Flowable;

/**
 * Created by Lee on 2018/2/28 0028.
 */

public class MarketFragment extends BaseFragment<MainActivity, MarketFragmentPst> {

    @BindView(R.id.slidingTabLayout)
    SlidingTabLayout slidingTabLayout;

    @BindView(R.id.viewPager)
    ViewPagerSlide viewPager;
    private List<String> titles = new ArrayList<>();
    private List<Fragment> fragments = new ArrayList<>();

    @Override
    public int getLayoutId() {
        return R.layout.fragment_market;
    }

    public static MarketFragment newInstance() {
        return new MarketFragment();
    }

    @Override
    public MarketFragmentPst getPresenter() {
        return new MarketFragmentPst();
    }

    @Override
    public void initView() {
        RxBus.getDefault().register(this);

        titles.add("USDT");
        titles.add("BTC");
        titles.add("ETH");
        Flowable.fromIterable(titles)
                .subscribe(item -> fragments.add(MarketListFragment.newInstance(item)));
        viewPager.setAdapter(new MyFragmentPagerAdapter(getChildFragmentManager(), titles, fragments));
        slidingTabLayout.setViewPager(viewPager);
//        slidingTabLayout.setVisibility(View.GONE);
    }

    @Subscribe(code = Constants.DRAWER_OPEN, threadMode = ThreadMode.MAIN)
    public void changeCoin() {
        if (mActivity.index == 2) {
            if (slidingTabLayout != null) {
                slidingTabLayout.setVisibility(View.GONE);
                viewPager.setCurrentItem(0);
                viewPager.setSlide(false);
            }
        } else if (mActivity.index == 1) {
            if (slidingTabLayout != null) {
                slidingTabLayout.setVisibility(View.VISIBLE);
                viewPager.setSlide(true);
            }


        }
    }
}
